package cn.wolfcode.transfer.exception;

import cn.wolfcode.transfer.qo.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//该类对所有控制器进行增强
@ControllerAdvice
public class ControllerMy {
    //代表该方法时用来捕获异常的(捕获的异常类型)
    @ExceptionHandler(Exception.class)
    //使用HandlerMethod获取出现异常的方法对象,可从其中获取所贴注解
    public String RuntimeException(Exception e, HandlerMethod handlerMethod, HttpServletResponse response){
        e.printStackTrace();
        return "/common/error";
    }
    @ExceptionHandler(MyLoginException.class)
    //使用HandlerMethod获取出现异常的方法对象,可从其中获取所贴注解
    @ResponseBody
    public JsonResult LoginException(Exception e, HandlerMethod handlerMethod, HttpServletResponse response){
        return new JsonResult(false,e.getMessage());
    }
}
